rm(list=ls())

library(tidyr)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(tidyverse)
library(reshape2)
library(readr)

#Directory: replication folder

borders_unga_speeches <- read_csv('kw_relevance_predicted_set.csv') %>% 
  select(-starts_with('x')) %>% filter(Predicted == 1)

terms <- c('crim|smuggl|traffic', 'migra|refugee', 'demarc', 'terroris|rebel|guerilla|non-state', 'state', 'international' ,'war', 'disease|pandemic|infection|health|vaccin|virus')#, 'ethnic|minorit')
for(term in terms){
  borders_unga_speeches[,term] <- grepl(pattern = term, x = borders_unga_speeches$text, ignore.case = TRUE)
}

term_data <- borders_unga_speeches %>% group_by(year) %>% dplyr::summarise(`crim|smuggl|traffic` = sum(`crim|smuggl|traffic`)/n(),
                                                                    `migra|refugee` = sum(`migra|refugee`)/n(),
                                                                    `disease` = sum(`disease|pandemic|infection|health|vaccin|virus`)/n(),
                                                                    terroris = sum(`terroris|rebel|guerilla|non-state`)/n(),
                                                                    demarc = sum(demarc)/n(),
                                                                    state = sum(state)/n(),
                                                                    international = sum(international)/n(),
                                                                    war = sum(war)/n())

term_data <- term_data %>% dplyr::rename(Crime = `crim|smuggl|traffic`, Migration = `migra|refugee`, Disease = disease, Terrorism = terroris, Demarcation = demarc, 
                                         State = state, International = international, War = war)

term_data_condensed <- borders_unga_speeches %>% group_by(year) %>% dplyr::summarise(`crim|smuggl|traffic` = sum(`crim|smuggl|traffic`)/n(),
                                                                                     `migra|refugee` = sum(`migra|refugee`)/n(),
                                                                                     terroris = sum(terroris)/n(),
                                                                                     demarc = sum(demarc)/n(),
                                                                                     state = sum(state)/n(),
                                                                                     international = sum(international)/n(),
                                                                                     war = sum(war)/n())
                                                                    

to_plot <- melt(term_data, id.vars='year')

gg <- ggplot(to_plot, aes(x=year, y=value, color=variable)) + 
  geom_smooth() + geom_point() + 
  facet_wrap(~variable, nrow = 4, dir='v') + 
  theme(legend.position = 'none',
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + xlab(NULL) + ylab(NULL)

# Figure 4
ggsave(gg, filename = 'Figure 4.png', dpi=1000, width = 4, height=6.5)



